package com.weitu.emergency.controller;


import com.alibaba.fastjson.JSON;
import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.emergency.entity.RehearsalPlan;
import com.weitu.emergency.entity.RehearsalUser;
import com.weitu.emergency.service.RehearsalPlanService;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.user.entity.User;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017-05-02
 */
@Controller
@RequestMapping("emergency/rehearsalPlan")
public class RehearsalPlanController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "emergency/rehearsalPlan";

    private final RehearsalPlanService rehearsalPlanService;
    private final AccessoryUtilService accessoryUtilService;

    @Autowired
    public RehearsalPlanController(RehearsalPlanService rehearsalPlanService, AccessoryUtilService accessoryUtilService) {
        this.rehearsalPlanService = rehearsalPlanService;
        this.accessoryUtilService = accessoryUtilService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listView() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName(MODULE_BATH_PATH + "/list");

        return modelAndView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addView() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/add");
        return modelAndView;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editView(String id) {

        RehearsalPlan plan = rehearsalPlanService.findById(id);
        //查询该业务对应的附件信息
        List<AccessoryPackageInfo> accessoryList = accessoryUtilService.queryAccessoryByBusinessId(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");
        modelAndView.addObject("rehearsalPlan", plan);

        modelAndView.addObject("accessoryList", accessoryList);

        return modelAndView;
    }


    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public ModelAndView detailsView(String id) {

        RehearsalPlan plan = rehearsalPlanService.findById(id);
        //查询该业务对应的附件信息
        List<AccessoryPackageInfo> accessoryList = accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/details");
        modelAndView.addObject("rehearsalPlan", plan);

        modelAndView.addObject("accessoryList", accessoryList);

        return modelAndView;
    }


    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/findById")
    public RehearsalPlan findById(String id) {
        return rehearsalPlanService.findById(id);
    }

    @ResponseBody
    @RequestMapping("/findByParam")
    public Object findByParam(HttpServletRequest request) {

        Map<String, Object> paramMap = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(paramMap);

        Integer totalNum = rehearsalPlanService.findByParamCount(paramMap);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);

        List<RehearsalPlan> emergencyPlanList = rehearsalPlanService.findByParam(paramMap);

        return pagination.flushDataGridData(emergencyPlanList);
    }


    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @SystemLog(module = "应急管理_演练计划", methods = "添加演练计划")
    public AjaxResult add(RehearsalPlan rehearsalPlan,HttpServletRequest request) {

        User loginUser = getSessionUser();

        //获得主键
        String id = IDGenerator.getId();
        rehearsalPlan.setId(id);
        rehearsalPlan.setAddDeptId(loginUser.getDepartment().getId());
        rehearsalPlan.setAddUserId(loginUser.getId());
        rehearsalPlan.setAddTime(DateTimeUtil.formatterDateTime());
        Integer isWrite=rehearsalPlan.getIsWrite();
        List<RehearsalUser> rehearsalUserList=null;
        if(isWrite==1){
            rehearsalPlan.getRehearsalInfo().setId(IDGenerator.getId());
            rehearsalPlan.getRehearsalInfo().setRehearsalPlanId(id);

            rehearsalPlan.getRehearsalUserOther().setId(IDGenerator.getId());
            rehearsalPlan.getRehearsalUserOther().setRehearsalPlanId(id);
            //参演人员
            String rehearsalUserJson=request.getParameter("rehearsalUser");
            rehearsalUserList = JSON.parseArray(rehearsalUserJson,RehearsalUser.class);
            for (RehearsalUser rehearsalUser:rehearsalUserList){
                rehearsalUser.setId(IDGenerator.getId());
                rehearsalUser.setRehearsalPlanId(id);
            }
            //保存关联
            AccessoryUtil.saveAccerssoryInfo(id);
        }else {
            rehearsalPlan.setRehearsalUserOther(null);
            rehearsalPlan.setRehearsalInfo(null);
        }

        Integer result = rehearsalPlanService.add(rehearsalPlan,rehearsalUserList);

        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @SystemLog(module = "应急管理_演练计划", methods = "编辑演练计划")
    public AjaxResult edit(RehearsalPlan rehearsalPlan,HttpServletRequest request) {

        User loginUser = getSessionUser();

        //获得主键
        String id = rehearsalPlan.getId();
        rehearsalPlan.setAddDeptId(loginUser.getDepartment().getId());
        rehearsalPlan.setAddUserId(loginUser.getId());
        rehearsalPlan.setAddTime(DateTimeUtil.formatterDateTime());
        Integer isWrite=rehearsalPlan.getIsWrite();
        List<RehearsalUser> rehearsalUserList=null;
        Map record=null;
        if(isWrite==1){
            rehearsalPlan.getRehearsalInfo().setId(IDGenerator.getId());
            rehearsalPlan.getRehearsalInfo().setRehearsalPlanId(id);

            rehearsalPlan.getRehearsalUserOther().setId(IDGenerator.getId());
            rehearsalPlan.getRehearsalUserOther().setRehearsalPlanId(id);
            //参演人员
            String rehearsalUserJson=request.getParameter("rehearsalUser");
            rehearsalUserList = JSON.parseArray(rehearsalUserJson,RehearsalUser.class);
            for (RehearsalUser rehearsalUser:rehearsalUserList){
                rehearsalUser.setId(IDGenerator.getId());
                rehearsalUser.setRehearsalPlanId(id);
            }
            //发现问题
            String problemJson=request.getParameter("problem");
            List<Map> taskList=JSON.parseArray(problemJson,Map.class);
            for (Map map:taskList){
                map.put("id",IDGenerator.getId());
                map.put("taskId",id);
                map.put("taskSource","应急演练");
                map.put("delState",0);
                if (null==map.get("addUserId")){
                    map.put("addUserId",loginUser.getId());
                }
                if (null==map.get("addDeptId")){
                    map.put("addDeptId",loginUser.getDepartment().getId());
                }
                if (null==map.get("addTime")){
                    map.put("addTime",DateTimeUtil.formatterDateTime());
                }

            }
            record=new HashMap();
            record.put("taskList",taskList);

            //保存关联
            AccessoryUtil.saveAccerssoryInfo(id);
        }else {
            rehearsalPlan.setRehearsalUserOther(null);
            rehearsalPlan.setRehearsalInfo(null);
        }

        Integer result = rehearsalPlanService.updateById(rehearsalPlan,rehearsalUserList,record,id);

        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @SystemLog(module = "应急管理_演练计划", methods = "删除演练计划")
    public AjaxResult delete() {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);

        String id=paramMap.get("id").toString();

        Integer result = rehearsalPlanService.deleteById(paramMap,id);

        return resultAjaxResult(result);
    }




    /**
     * 处理返回参数
     *
     * @param result 返回值
     * @return 返回值
     */
    private AjaxResult resultAjaxResult(Integer result) {
        AjaxResult ajaxResult = new AjaxResult();
        if (result > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("操作成功");
        }
        return ajaxResult;
    }


}
